# -*- coding: UTF-8 -*-
from typing import List
from leetcode_helper import TestcaseHelper, SolutionBase


class Solution(SolutionBase):
    # 解题的方法，命名和Leetcode的保持一致即可
    def isValid(self, code: str) -> bool:
        tags = []
        is_find_tag = False
        i, code_len = 0, len(code)
        if code_len < 3:
            return False

        while i < code_len:
            ch = code[i]
            if code[i] != '<':
                if not tags:
                    return False
                i += 1
                continue

            if code[i] == '<':
                if code[i + 1] == '!':
                    if not tags:
                        return False

                    cdata = code[i + 2: i + 9]
                    if cdata != '[CDATA[':
                        return False

                    j = code.find(']]>', i)
                    if j == -1:
                        return False

                    i = j + 3
                elif code[i + 1] == '/':
                    j = code.find('>', i)
                    if j == -1:
                        return False

                    tag_name = code[i + 2: j]
                    if 1 <= len(tag_name) <= 9 and tag_name.isalpha() and tag_name.isupper():
                        if not tags:
                            return False
                        prev_tag_name = tags.pop()
                        if prev_tag_name != tag_name:
                            return False
                    else:
                        return False
                    i = j + 1
                else:
                    j = code.find('>', i)
                    if j == -1:
                        return False
                    tag_name = code[i + 1: j]
                    if 1 <= len(tag_name) <= 9 and tag_name.isalpha() and tag_name.isupper():
                        if not is_find_tag:
                            tags.append(tag_name)
                            is_find_tag = True
                        else:
                            if not tags:
                                return False
                            tags.append(tag_name)
                    else:
                        return False

                    i = j + 1

        return not tags and is_find_tag


if __name__ == '__main__':


    # 按照leetcode的题目说明，将基本的test case填入
    # 每条case的最后一个元素是正确的结果，即期待的结果
    # 使用元组来保存输入的数据，期待的结果
    testcases = [
        ('<A></A><B></B>', False),
        ("<A><![CDATA[abc]]></A>", True),
        ("abc", False),
        ("<DIV>This is the first line <![CDATA[<div>]]></DIV>", True),
        ("<DIV>>>  ![cdata[]] <![CDATA[<div>]>]]>]]>>]</DIV>", True),
        ("<A>  <B> </A>   </B>", False)
    ]

    for case_item in testcases:
        solution = Solution()
        # 获取测试用的case和期待的执行结果
        input1 = case_item[0]
        expect = case_item[len(case_item) - 1]

        exec_result = solution.isValid(input1)

        # 判断执行结果，输出提示信息
        check_result = solution.check_result(expect, exec_result)
        TestcaseHelper.print_case(check_result, case_item, exec_result)

